package com.iskyonline.skyonlieapp.controller;

import com.iskyonline.skyonlieapp.service.IndexService;
import com.iskyonline.skyonlieapp.util.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Controller
public class PageController extends BaseController {

    @Autowired
    private IndexService indexService;

    @RequestMapping("/bo/toHomeBanner.html")
    public String toHomeBanner() {
        return "bo/homeBanner";
    }

    @RequestMapping("/bo")
    public String bo() {
        return "bo/login";
    }

    @RequestMapping("/bo/welcome.html")
    public String boWelcome() {
        return "bo/welcome";
    }

    @RequestMapping("/bo/index.html")
    public String boIndex() {
        return "bo/index";
    }

    @RequestMapping("/bo/toHomePicture.html")
    public String toHomePicture() {
        return "bo/homePicture";
    }

    @RequestMapping("/bo/waibaofuwu.html")
    public String waibaofuwu() {
        return "bo/waibaofuwu";
    }

    @RequestMapping("/bo/homeInfo.html")
    public String homeInfo() {
        return "bo/homeInfo";
    }

    @RequestMapping("/bo/hezuodanwei.html")
    public String hezuodanwei() {
        return "bo/hezuodanwei";
    }

    @RequestMapping("/qiyewenhua.html")
    public String qiyewenhua(HttpServletRequest request) {
        requstSave(request, indexService);
        return "qiyewenhua";
    }

    @RequestMapping("/zhaopin.html")
    public String zhaopin(HttpServletRequest request) {
        requstSave(request, indexService);
        return "zhaopin";
    }

    @RequestMapping("/lianxiwomen.html")
    public String lianxiwomen(HttpServletRequest request) {
        requstSave(request, indexService);
        return "lianxiwomen";
    }

    @RequestMapping("/guanyuwomen.html")
    public String guanyuwomen(HttpServletRequest request) {
        requstSave(request, indexService);
        return "guanyuwomen";
    }

    @RequestMapping("/waibaofuwu.html")
    public String waibaofuwu(HttpServletRequest request) {
        requstSave(request, indexService);
        return "waibaofuwu";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {

        try {
            if (file.isEmpty()) {
                System.out.println(">>>>>>>>>>upload() empty");
                return Collections.emptyMap();
            }

            String fileName = IDUtils.createImageName("product");
            File uploadedFile = new File(filePath + fileName);
            file.transferTo(uploadedFile);

            List<String> list = new ArrayList<>(1);
            list.add(fileUrl + fileName);

            Map<String, Object> map = new HashMap<>();
            map.put("errno", 0);
            map.put("data", list);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @RequestMapping("/pic/{filename}")
    @ResponseBody
    public void showPic(@PathVariable("filename") String filename, HttpServletResponse response) {

        response.setContentType("image/png");
        try(OutputStream os = response.getOutputStream();

            InputStream is = new FileInputStream(filePath + filename)) {

            byte[] data = new byte[1024];
            int length = -1;
            while( -1 != (length = is.read(data))) {
                os.write(data, 0, length);
                os.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
